package com.solitaire.and;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;

public class SoliCocosBridge {

    public static WeakReference<Activity> mainGameActivity;
    private Method evalString;
    private Method runOnGLThread;
    public Context context;

    public static SoliCocosBridge getInstance() {
        return InstanceHolder.SOLI_COCOS_BRIDGE;
    }

    private static class InstanceHolder {
        private static final SoliCocosBridge SOLI_COCOS_BRIDGE = new SoliCocosBridge();
    }

    public void init(Context ctx) {
        try {
            context = ctx.getApplicationContext();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMainGameActivity(Activity activity) {
        mainGameActivity = new WeakReference<>(activity);
        getRunOnGLThread();
        getEvalString();
    }

    private void getEvalString() {
        try {
            Class<?> clzBridge = Class.forName(SoliConstant.LIB_PATH.concat(".Cocos2dxJavascriptJavaBridge"), false, context.getClassLoader());
            evalString = clzBridge.getDeclaredMethod("evalString", String.class);
            evalString.setAccessible(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static final Handler handler = new Handler(Looper.getMainLooper());

    public void videoCallback(String action, String jsonData) {
        if (mainGameActivity == null || mainGameActivity.get() == null || context == null) {
            return;
        }
        handler.post(() -> {
            try {
                runOnGLThread.invoke(mainGameActivity.get(), (Runnable) () -> {
                    callback(action, jsonData);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void getRunOnGLThread() {
        try {
            Class<?> clzCocos2dAct = Class.forName(SoliConstant.LIB_PATH.concat(".Cocos2dxActivity"), false, context.getClassLoader());
            runOnGLThread = clzCocos2dAct.getDeclaredMethod("runOnGLThread", Runnable.class);
            runOnGLThread.setAccessible(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void callback(String action, String jsonData) {
        try {
            evalString.invoke(null, String.format("window.VideoCallback('%s','%s')", action, jsonData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(final String message) {
        if (mainGameActivity == null || mainGameActivity.get() == null) {
            return;
        }
        Activity activity =mainGameActivity.get();
        activity.runOnUiThread(() -> Toast.makeText(activity, message, Toast.LENGTH_SHORT).show());
    }

    public static void playAd(String adSource){
        if (mainGameActivity == null || mainGameActivity.get() == null) {
            return;
        }
        SoliMaxReward.getInstance().showAd(SoliCocosBridge.mainGameActivity.get(), adSource);
    }
}
